﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Data {

    public class Functions {

        public static String SetValueMaxLength (String forValue, Int32 maximumLength) {

            String value = ((forValue != null) ? forValue : String.Empty); // NO NULLS ALLOWED 

            value = value.PadRight (maximumLength).Substring (0, maximumLength).TrimEnd ();

            return value;

        }

        public static String EnumerationToString (Object value) {

            String enumerationString = value.ToString ();

            if (!String.IsNullOrEmpty (enumerationString)) {

                enumerationString = System.Text.RegularExpressions.Regex.Replace (enumerationString, @"((?<=\p{Ll})\p{Lu})|((?!\A)\p{Lu}(?>\p{Ll}))", " $0");

                enumerationString = enumerationString.Trim ();

            }

            return enumerationString;

        }

        public static String PascalString (String value) {

            String enumerationString = value;

            if (!String.IsNullOrEmpty (enumerationString)) {

                enumerationString = System.Text.RegularExpressions.Regex.Replace (enumerationString, @"((?<=\p{Ll})\p{Lu})|((?!\A)\p{Lu}(?>\p{Ll}))", " $0");

                enumerationString = enumerationString.Trim ();

            }

            return enumerationString;

        }

        public static Int32 CurrentAge (DateTime? forBirthDate) {

            if (!forBirthDate.HasValue) { return -1; }

            Int32 currentAge = 0;

            DateTime birthDay;


            currentAge = DateTime.Today.Year - forBirthDate.Value.Year;

            birthDay = new DateTime (DateTime.Today.Year, forBirthDate.Value.Month, (((forBirthDate.Value.Month == 2) && (forBirthDate.Value.Day == 29)) ? 28 : forBirthDate.Value.Day));

            if (DateTime.Today.CompareTo (birthDay) < 1) { currentAge = currentAge - 1; }

            return currentAge;

        }

    }

}
